/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yaohe.consumer.test;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;

import java.util.ArrayList;
import java.util.Collection;

import com.yaohe.consumer.R;

/**
 * The main settings activity.
 *
 * @author dswitkin@google.com (Daniel Switkin)
 */
public final class PreferencesActivity extends PreferenceActivity
    implements OnSharedPreferenceChangeListener {

  public static final String KEY_SHOW_INVALIDATE_YAOHE = "preferences_show_invalidate_discount";
  public static final String KEY_AUTO_DEL_INVALIDATE_YAOHE = "preferences_auto_delete_invalidate_discount";
  public static final String KEY_REC_SHOP_COLLECTION_YAOHE = "preferences_rec_only_collection_shop";
  public static final String KEY_REC_DISTANCE_YAOHE = "preferences_rec_yaohe_distance";

  public static final String KEY_AUTO_REC_SHOP = "preferences_auto_rec_shop_yaohe";

  private CheckBoxPreference showInvalidateYaohe;
  private CheckBoxPreference autoDeleteInvalidate;
  private CheckBoxPreference recOnlyCollectionShop;

  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    
    this.setTitle(R.string.personal_love_setting);
    
    addPreferencesFromResource(R.xml.preferences);

    PreferenceScreen preferences = getPreferenceScreen();
    preferences.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    showInvalidateYaohe = (CheckBoxPreference) preferences.findPreference(KEY_SHOW_INVALIDATE_YAOHE);
    autoDeleteInvalidate = (CheckBoxPreference) preferences.findPreference(KEY_AUTO_DEL_INVALIDATE_YAOHE);
    recOnlyCollectionShop = (CheckBoxPreference) preferences.findPreference(KEY_REC_SHOP_COLLECTION_YAOHE);
    disableLastCheckedPref();
  }

  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    disableLastCheckedPref();
  }

  private void disableLastCheckedPref() {
    Collection<CheckBoxPreference> checked = new ArrayList<CheckBoxPreference>(3);
    if (showInvalidateYaohe.isChecked()) {
      checked.add(showInvalidateYaohe);
    }
    if (autoDeleteInvalidate.isChecked()) {
      checked.add(autoDeleteInvalidate);
    }
    if (recOnlyCollectionShop.isChecked()) {
      checked.add(recOnlyCollectionShop);
    }
    boolean disable = checked.size() < 2;
    for (CheckBoxPreference pref : new CheckBoxPreference[] {showInvalidateYaohe, autoDeleteInvalidate, recOnlyCollectionShop}) {
      pref.setEnabled(!(disable && checked.contains(pref)));
    }
  }
}
